package com.system.enterprisemanagementsystem.controller;


import com.system.enterprisemanagementsystem.anno.LogOperation;
import com.system.enterprisemanagementsystem.pojo.Dept;
import com.system.enterprisemanagementsystem.pojo.Result;
import com.system.enterprisemanagementsystem.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     */
    @GetMapping
    public Result list(){
        log.info("查询部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    @DeleteMapping
    @LogOperation
    public Result delete(Integer id){
        log.info("根据id删除部门, id: {}" , id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    @LogOperation
    public Result addDept(@RequestBody Dept dept){
        log.info("根据部门名称添加部门,{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id来查找部门，id={}",id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @PutMapping
    @LogOperation
    public Result updateDept(@RequestBody Dept dept){
        log.info("根据id来更新部门名称，id={}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
